<template>
	<view class="page-index">
		<!-- 加载状态 -->
		<view v-if="loading" class="loading-box">
			<view class="loading">加载中...</view>
		</view>
		
		<!-- 订单详情 -->
		<view v-else-if="orderDetail" class="detail-content">
			<view class="title-box">
				{{orderDetail.order.goodsName}}
			</view>
			<view class="image-box">
				<image :src="orderDetail.order.goodsCoverPic" class="item-img" mode="aspectFill"></image>
			</view>
			<view class="title-box">
				收货地址
			</view>
			<view class="details-box">
				{{orderDetail.order.deliveryAddress}}
			</view>
			<view class="item-box">
				<view class="name-box">订单号</view>
				<view class="text">{{orderDetail.order.orderNo}}</view>
			</view>
			<view class="item-box">
				<view class="name-box">创建时间</view>
				<view class="text">{{orderDetail.order.cjTime}}</view>
			</view>
			<view class="item-box">
				<view class="name-box">下单时间</view>
				<view class="text">{{orderDetail.order.payTime}}</view>
			</view>
			<view class="item-box">
				<view class="name-box">商品数量</view>
				<view class="text">{{orderDetail.order.goodsNum}}</view>
			</view>
			<view class="item-box">
				<view class="name-box">支付方式</view>
				<view class="text">{{getPayTypeText(orderDetail.order.payType)}}</view>
			</view>
			<view class="item-box">
				<view class="name-box">订单类型</view>
				<view class="text">{{getOrderTypeText(orderDetail.order.type)}}</view>
			</view>
			
			<!-- 地图定位按钮 -->
			<view v-if="orderDetail.order.latitude && orderDetail.order.longitude" class="bnt-box">
				<view class="bnt" @click="location">
					<view class="icon">
						<image src="../../../../static/index/dizhi.png" class="icon-img" mode=""></image>
					</view>
					查看地图位置
				</view>
			</view>
		</view>
		
		<!-- 错误提示 -->
		<view v-else class="error-box">
			<view class="error-text">订单信息加载失败</view>
			<view class="retry-btn" @click="fetchOrderDetailData">重新加载</view>
		</view>
	</view>
</template>

<script>
	import { mapState, mapActions } from 'vuex'
	export default {
		data(){
			return{
				loading: false,
				orderNo: ''
			}
		},
		computed: {
			...mapState(['orderDetail'])
		},
		onLoad(params) {
			if(params.id) {
				this.orderNo = params.id
				this.fetchOrderDetailData()
			} else {
				uni.showToast({ title: '订单ID缺失', icon: 'none' })
				// 返回上一页
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
			}
		},
		methods:{
			...mapActions(['fetchOrderDetail']),
			
			// 获取订单详情数据
			fetchOrderDetailData() {
				this.loading = true
				this.fetchOrderDetail(this.orderNo).then(() => {
					this.loading = false
				}).catch(error => {
					console.error('获取订单详情失败:', error)
					this.loading = false
					uni.showToast({ title: '获取订单详情失败', icon: 'none' })
				})
			},
			
			// 查看地图位置
			location(){
				if(this.orderDetail && this.orderDetail.order.latitude && this.orderDetail.order.longitude) {
					uni.openLocation({
						latitude: Number(this.orderDetail.order.latitude),
						longitude: Number(this.orderDetail.order.longitude),
						name: this.orderDetail.order.goodsName,
						address: this.orderDetail.order.deliveryAddress,
						success: function () {
							console.log('打开地图成功');
						},
						fail: function (err) {
							console.error('打开地图失败:', err)
							uni.showToast({ title: '打开地图失败', icon: 'none' })
						}
					});
				}
			},
			
			// 获取支付方式文本
			getPayTypeText(payType) {
				const payTypeMap = {
					1: '微信',
					2: '支付宝',
					3: '积分'
				}
				return payTypeMap[payType] || '其他'
			},
			
			// 获取订单类型文本
			getOrderTypeText(type) {
				const typeMap = {
					1: '会员',
					2: '福利商品',
					3: '生活商品'
				}
				return typeMap[type] || '其他'
			}
		}
	}
</script>

<style scoped lang="scss">
	.page-index {
		min-height: 100vh;
		background: #FFFFFF;
		position: relative;
	}
	
	/* 加载状态样式 */
	.loading-box {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 50vh;
		.loading {
			font-size: 32rpx;
			color: #888888;
		}
	}
	
	/* 错误提示样式 */
	.error-box {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 50vh;
		.error-text {
			font-size: 32rpx;
			color: #888888;
			margin-bottom: 40rpx;
		}
		.retry-btn {
			padding: 20rpx 60rpx;
			background-color: #1b6cfc;
			color: #ffffff;
			font-size: 28rpx;
			border-radius: 40rpx;
		}
	}
	
	/* 详情内容样式 */
	.detail-content {
		padding: 30rpx 41rpx 230rpx 30rpx;
		.title-box {
			font-size: 37rpx;
			font-family: Microsoft YaHei;
			font-weight: 500;
			color: #000000;
			margin-top: 20rpx;
		}
		.image-box {
			display: flex;
			justify-content: center;
			padding: 30rpx 10rpx 50rpx 10rpx;
			.item-img {
				width: 680rpx;
				height: 378rpx;
				border-radius: 19rpx;
				object-fit: cover;
			}
		}
		.details-box {
			margin-top: 40rpx;
			margin-bottom: 80rpx;
			font-size: 28rpx;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: #353535;
			line-height: 48rpx;
			word-break: break-all;
		}
		.item-box {
			margin-top: 40rpx;
			display: flex;
			align-items: flex-start;
			padding-bottom: 30rpx;
			border-bottom: 1rpx solid #f0f0f0;
			.name-box {
				font-size: 28rpx;
				font-family: Microsoft YaHei;
				font-weight: 400;
				color: #888888;
				width: 160rpx;
			}
			.text {
				margin-left: 30rpx;
				flex: 1;
				font-size: 28rpx;
				font-family: Microsoft YaHei;
				font-weight: 400;
				color: #353535;
				word-break: break-all;
			}
		}
		.bnt-box {
			position: fixed;
			bottom: 0rpx;
			left: 0;
			width: 750rpx;
			height: 160rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background-color: #FFFFFF;
			border-top: 1rpx solid #f0f0f0;
			.bnt {
				width: 680rpx;
				height: 90rpx;
				background-color: #1b6cfc;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #ffffff;
				font-size: 28rpx;
				font-weight: 500;
				border-radius: 45rpx;
				.icon {
					width: 40rpx;
					height: 40rpx;
					margin-right: 12rpx;
					.icon-img {
						width: 100%;
						height: 100%;
					}
				}
			}
		}
	}
</style>
